<?php

namespace App\Http\Controllers\Api\Admin;

use App\Http\Controllers\Api\Controller;
use App\Models\Menu;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MenuController extends Controller
{

  //菜单列表
  public function index(Request $request)
  {
    $menus = Menu::getList($request);
    return $this->success($menus);
  }

  // 菜单详情
  public function detail($id)
  {
    $menuInfo = Menu::getInfo($id);
    return $this->success($menuInfo);
  }

  // 新增菜单
  public function add(Request $request)
  {
    $data = $request->all();
    $rule = [
      'name'      => 'required',
      'permission'=> 'required'    
    ];
    $message = [
      'name.required'       => '菜单名称不能为空',
      'permission.required' => '权限标识不能为空'    
    ];
    $validated = $this->validator($data, $rule, $message);
    if (is_bool($validated)) {
      $insertData = [
        'name' => $request->name,
        'parent_id' => $request->parent_id,
        'icon' => $request->icon,
        'path' => $request->path,
        'component' => $request->component,
        'is_iframe' => $request->is_iframe,
        'is_link' => $request->is_link,
        'menu_type' => $request->menu_type,
        'is_hide' => $request->is_hide,
        'permission' => $request->permission,
        'status' => $request->status,
        'sort' => $request->sort,
        'remark' => $request->remark,
        'created_at' => date('Y-m-d H:i:s'),
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('menus')->insert($insertData);
      return $this->success();
    } else {
      return $validated;
    }
  }

  // 修改菜单
  public function update(Request $request)
  {
    $menuInfo = DB::table('menus')->where('id', $request->id)->first();
    if ($menuInfo && $menuInfo->id) {
      $updateData = [
        'name' => $request->name ?? $menuInfo->name,
        'parent_id' => $request->parent_id ?? $menuInfo->parent_id,
        'icon' => $request->icon ?? $menuInfo->icon,
        'path' => $request->path ?? $menuInfo->path,
        'component' => $request->component ?? $menuInfo->component,
        'is_iframe' => $request->is_iframe ?? $menuInfo->is_iframe,
        'is_link' => $request->is_link ?? $menuInfo->is_link,
        'menu_type' => $request->menu_type ?? $menuInfo->menu_type,
        'is_hide' => $request->is_hide ?? $menuInfo->is_hide,
        'permission' => $request->permission ?? $menuInfo->permission,
        'status' => $request->status ?? $menuInfo->status,
        'sort' => $request->sort ?? $menuInfo->sort,
        'remark' => $request->remark ?? $menuInfo->remark,
        'updated_at' => date('Y-m-d H:i:s'),
      ];
      $result = DB::table('menus')->where('id', $request->id)->update($updateData);
      if ($result) {
        return $this->success();
      }
      return $this->failed('修改失败');
    } else {
      return $this->failed('菜单不存在');
    }
  }

  // 删除菜单
  public function delete($id)
  {
    $result = DB::table('menus')->where('id', $id)->delete();
    if ($result) {
      return $this->success();
    }
    return $this->failed('删除失败');
  }
}
